package com.flying.other;

import com.flying.map.Person;
import org.junit.jupiter.api.Test;

import javax.lang.model.SourceVersion;
import java.util.*;

/**
 * @ClassName Collections
 * @Author flying
 * @Date 2025/1/27 19:27
 * @Description Collections工具类的使用
 */
public class CollectionsTest {

    @Test
    void testCollections(){

        List<String> list = new ArrayList<>();

        list.add("a");
        list.add("c");
        list.add("b");
        list.add("d");
        list.add("e");


        for (String s : list) {
            System.out.println("s = " + s);
        }

        System.out.println("==========对集合进行排序==============");

        Collections.sort(list);
//
//
        for (String s : list) {
            System.out.println("s = " + s);
        }
//
//        System.out.println("===========将集合中的元素随机排列=============");
//
//
//        Collections.shuffle(list);
//
//        for (String s : list) {
//            System.out.println("s = " + s);
//        }

        System.out.println("============反转集合============");

        // 反转集合
        Collections.reverse(list);
        for (String s : list) {
            System.out.println("s = " + s);
        }


        System.out.println("===============填充集合==================");
//        Collections.fill(list,"great");
//
//        for (String s : list) {
//            System.out.println("s = " + s);
//        }



        System.out.println("===============二分查找==================");
        // 二分查找必须先对容器元素进行排序
        Collections.sort(list);
        int b = Collections.binarySearch(list, "b");
        System.out.println("b = " + b);
        for (String s : list) {
            System.out.println("s = " + s);
        }

    }











    /**
     * 遍历集合的方法总结
     */
    @Test
    void testList(){
        List<String> list=new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("E");
        list.add("D");
        list.add("F");


        // 普通for循环
        System.out.println("========================普通for循环 ==================");
        for (int i=0;i<list.size();i++){
            String s = list.get(i);
            System.out.println("s = " + s);
        }

        System.out.println("========================增强for循环 ==================");

        for (String s : list) {
            System.out.println("s = " + s);
        }



        System.out.println("=======================迭代器==================");

        System.out.println("=======================while循环===============");
        Iterator<String> iterator = list.iterator();

        while (iterator.hasNext()){
            String next = iterator.next();
            System.out.println("next = " + next);
        }

        System.out.println("==============for循环====================");
        for (Iterator<String> iterator1=list.iterator();iterator1.hasNext();){
            String next = iterator1.next();
            System.out.println("next = " + next);

        }

    }


    /**
     * Set集合遍历方式： 1.增强for循环； 2. 迭代器遍历
     */
    @Test
    void testSet(){
        Set<String> set=new HashSet<>();
        set.add("A");
        set.add("B");
        set.add("E");
        set.add("D");
        set.add("D");
        set.add("F");

        // 增强for循环

        System.out.println("==================增强for循环==========================");
        for (String s : set) {
            System.out.println("s = " + s);
        }


        System.out.println("==================迭代器遍历==========================");
        Iterator<String> iterator = set.iterator();
        while(iterator.hasNext()){
            String next = iterator.next();
            System.out.println("next = " + next);
        }


    }

    /**
     * Map遍历两种方式： 遍历 keySet     ;    遍历entrySet
     */
    @Test
    void testMap(){

        Map<Person,Integer>   map=new TreeMap<>();


        Person p1 = new Person("张三", 12);
        Person p2 = new Person("王柳", 42);
        Person p3 = new Person("荆轲", 12);
        Person p4 = new Person("刘邦", 18);
        Person p5 = new Person("百里守约", 76);


        map.put(p1,13);
        map.put(p2,32);
        map.put(p3,23);
        map.put(p4,51);
        map.put(p5,9);


        System.out.println("==================遍历keySet================");
        Set<Person> people = map.keySet();

        for (Person person : people) {
            System.out.println("person = " + person);
        }
        System.out.println("=====================迭代器===================");

        Iterator<Person> iterator = people.iterator();
        while (iterator.hasNext()){
            Person next = iterator.next();
            System.out.println("next = " + next);
        }


        System.out.println("==================遍历entrySet=====================");
        Set<Map.Entry<Person, Integer>> entries = map.entrySet();

        for (Map.Entry<Person, Integer> entry : entries) {
            Person key = entry.getKey();
            System.out.println("key = " + key);
            Integer value = entry.getValue();
            System.out.println("value = " + value);
        }

        System.out.println("=====================迭代器===================");
        Iterator<Map.Entry<Person, Integer>> iterator1 = entries.iterator();


        while (iterator1.hasNext()){
            Map.Entry<Person, Integer> next = iterator1.next();

            System.out.println("key = " + next.getKey());
            System.out.println("value = " + next.getValue());
        }


    }














}
